package com.pg.ffmpeg;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.pg.ffmpeg.bean.VideoInfo;
import com.pg.ffmpeg.util.Gutil;

public class MainActivity extends AppCompatActivity {

    private int PICK_VIDEO_REQUEST = 0x2;
    private String mVideoPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, PICK_VIDEO_REQUEST);
            }
        });
        checkPermission();
    }

    private void checkPermission() {
        int permissions = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permissions != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    1
            );
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_VIDEO_REQUEST && resultCode == RESULT_OK && null != data) {
            Uri selectedVideo = data.getData();
            String[] filePathColumn = {MediaStore.Video.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedVideo,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            mVideoPath = cursor.getString(columnIndex);
//            mVideoPath = "/sdcard/Download/gamepp/KSP.mkv";
            cursor.close();
            updateVideo();
        }
    }

    private void updateVideo() {
//        Bitmap videoFrame = MediaTool.getVideoFrame(mVideoPath, 2000000);
//        mIvCover.setImageBitmap(videoFrame);
        VideoInfo mInfo = RecodeUtil.getInstance().getVideoInfo(mVideoPath);
        Log.i("fred","VideoInfo:"+(new Gson()).toJson(mInfo));
//        mTvResolution.setText("分辨率：" + mInfo.width + "x" + mInfo.height);
//        mTvBitrate.setText("码率：" + Gutil.bitrateFormat(mInfo.bitrate));
//        mTvFps.setText("FPS：" + mInfo.fps);
//        mTvDuration.setText("视频时长：" + Gutil.parseTime((int) mInfo.duration/1000));
//        mTvVcodec.setText("Video Codec: " + mInfo.videoCodec);
//        mTvRotation.setText("Video Rotation: " + mInfo.rotation+"°");
//
//        mEditTargetBitrate.setText("4000");
//        mEditTargetFPS.setText("30");
////        mEditTargetWidth.setText(mInfo.width + "");
////        mEditTargetHeight.setText(mInfo.height + "");
//        String path = getVideoPath() + getDetailTime() + ".mp4";
//        mEditSavePath.setText(path);
    }
}
